/**
 * Created by Administrator on 2018/12/29.
 */
var mouldNo,mouldColor,mouldName,spec,startTime,endTime,total;
var imgObj,max=0,min=1000,cur;//查询功能：模具列表
$(function(){
    initComponent();
    queryMouldFunction();
    changImg();
    //fancyInit();
});

function initComponent() {
    var curr_time = new Date();
    var strDate = (curr_time.getFullYear()-2) + "-";
    strDate += curr_time.getMonth() + 1 + "-";
    strDate += curr_time.getDate() + " 00:00:00";
    $("#startTime").datetimebox("setValue", strDate);
    $("#endTime").datetimebox("setValue", addDate(curr_time, 0) + " 23:59:59");
}

// 查询和重置
var queryMouldFunction = function () {
    $("#queryMouldBtn").click(function () {
        mouldNo = $("#mouldNo").val();
        mouldColor = $("#mouldColor").val();
        mouldName = $("#mouldName").val();
        spec = $("#spec").val();
        startTime = $("#startTime").val();
        endTime = $("#endTime").val();
        reload();
    });
}
// 查询时重新加载分页数据
var reload = function () {
    var url = "/selectMouldByPage/mobile?pageNo=1";
    if(null != mouldNo && "" != mouldNo) {
        url = url + "&mouldNo=" + mouldNo;
    }
    if(null != mouldColor && "" != mouldColor) {
        url = url + "&mouldColor=" + mouldColor;
    }
    if(null != mouldName && "" != mouldName) {
        url = url + "&mouldName=" + mouldName;
    }
    if(null != spec && "" != spec) {
        url = url + "&spec=" + spec;
    }
    if(null != startTime && "" != startTime) {
        url = url + "&startTime=" + startTime;
    }
    if(null != endTime && "" != endTime) {
        url = url + "&endTime=" + endTime;
    }
    $('#content').panel('refresh', url);
    $.mobile.go("#p2");
    var parameter = new Object();
    if($("#mouldNo").val()!="") parameter.mouldNo = mouldNo;
    if($("#mouldColor").val()!="") parameter.mouldColor = mouldColor;
    if($("#mouldName").val()!="") parameter.mouldName = mouldName;
    if($("#spec").val()!="") parameter.spec = spec;
    if($("#startTime").val()!="") parameter.startTime = startTime;
    if($("#endTime").val()!="") parameter.endTime = endTime;
    var jsonstr = postSyncAjax("/countMould/mobile", parameter);
    total = jsonstr.data.total;
    $("#pagination").pagination({
        total:total,
        pageSize:3,
        /*layout:['list','sep','first','prev','next','last','sep',
         'manual','sep','links'],*/
        layout:['links'],
        beforePageText:'第',
        afterPageText:'页 共{pages}页',
        loading:false,
        dispalyMsg:'当前显示{from}-{to}条记录，共{total}条记录',
        onSelectPage: function (pageNumber, pageSize) {
            $(this).pagination('loading');
            $('#content').panel("refresh", "/selectMouldByPage/mobile?pageNo="+pageNumber+
                "&mouldNo="+mouldNo+"&mouldColor="+mouldColor+"&mouldName="+mouldName+
                "&spec="+spec+"&startTime="+startTime+"&endTime="+endTime);
            $(this).pagination('loaded');
        }
    });
}

//删除
var deleteMould = function(){
    $("#delete").click(function () {
        var rows = table.datagrid('getSelected');
        if (null == rows) {
            $.messager.alert("消息提示", "至少选择一条记录", "warning");
            return;
        } else {
            $.messager.confirm("确认对话框","确定要删除该模具信息？",function (r) {
                if(r){
                    var parameter = new Object();
                    parameter.id = rows.id;
                    var jsonstr = postSyncAjax("/deleteMould", parameter);
                    console.log(jsonstr);
                    if(jsonstr.success) {
                        //$.messager.alert("消息提示", "模具删除成功！", "warning");
                        //table.datagrid("reload");
                        showDialogWithFunc("模具删除成功("+jsonstr.msg+")！",function () {
                            table.datagrid("reload");
                         });
                    } else {
                        //$.messager.alert("消息提示", "模具删除成功！", "warning");
                        showDialogWithContent("模具删除失败："+jsonstr.msg);
                    }
                }
            });
        }
    });
}

// 修改模具
var editMould = function () {
    $("#edit").click(function () {
        var rows = table.datagrid('getSelected');
        if (null == rows) {
            $.messager.alert("消息提示", "至少选择一条记录", "warning");
            return;
        } else {
            //top.$('#cur_id').val(rows.id);
            //console.log(rows.id);
            removeTabs('sh-tabs','修改模具');
            createTabs('sh-tabs','/editMould/'+rows.id,'修改模具','menu3');
        }
    });
}

// 模具详情
var mouldDetail = function (id) {
    var url = "/mouldDetail/mobile/"+id;
    $('#content').panel('refresh', url);
    $.mobile.go("#p2");
}
var showMe = function (src) {
    $("#myImg").prop("src",src);
    $('#dlg1').dialog({
        align:'center',
        top:50
    }).dialog('open');
}
var changImg = function () {
    imgobj = $(".imgObj");
    $.each(imgobj,function (index, item) {
        if(max<index) max=index;
        if(min>index) min=index;
    });
    $("#myImg").click(function () {
        var thisSrc = $(this).prop("src");
        $.each(imgobj,function (index, item) {
            if(thisSrc == $(item).prop("src")) {
                cur = index;
            }
        });
        if(cur==max) {
            cur = min;
        } else {
            cur = cur+1;
        }
        $.each(imgobj,function (index, item) {
            if(index == cur) {
                $("#myImg").prop("src",$(item).prop("src"));
            }
        });
    });
}
var fancyInit = function () {
    $('.fancybox-buttons').fancybox({
        openEffect  : 'none',
        closeEffect : 'none',

        prevEffect : 'none',
        nextEffect : 'none',

        closeBtn  : false,

        helpers : {
            title : {
                type : 'inside'
            },
            buttons	: {}
        },

        afterLoad : function() {
            this.title = 'Image ' + (this.index + 1) + ' of ' + this.group.length + (this.title ? ' - ' + this.title : '');
        }
    });
}